package website.web;

//import java.io.IOException;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URL;
import java.util.Iterator;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.io.FeedException;
import com.sun.syndication.io.SyndFeedInput;
import com.sun.syndication.io.XmlReader;

@SuppressWarnings("serial")
public class HomeServlet extends HttpServlet {

	private Logger logger = Logger.getLogger(this.getClass());
	private RequestDispatcher homeJsp;
	
	@Override
	public void init(ServletConfig config) throws ServletException {
		ServletContext context = config.getServletContext();
		homeJsp = context.getRequestDispatcher("/WEB-INF/jsp/home.jsp");
	}	

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		logger.debug("Retrieving yahoo news feed.");
		URL url = new URL("http://rss.news.yahoo.com/rss/tech");
		SyndFeedInput syndFeedInput = new SyndFeedInput();
		SyndFeed syndFeed = null;
		XmlReader xmlReader = new XmlReader(url);
		try {
			syndFeed = syndFeedInput.build(xmlReader);
		} catch (IllegalArgumentException e) {
			logger.error("", e);
		} catch (FeedException e) {
			logger.error("", e);
		}
		
		/*
		resp.setContentType("text/html");
		PrintWriter writer = resp.getWriter();
		writer.println(syndFeed.getTitle());
		Iterator it = syndFeed.getEntries().iterator();
		while(it.hasNext()){
			SyndEntry entry = (SyndEntry) it.next();
			writer.printf("<a href= '%s'> %s </a>", entry.getLink(), entry.getTitle());
			writer.println("\t" + entry.getDescription().getValue() + "\n");
		}
		*/
		
		logger.debug("Forwarding to home.jsp");
		req.setAttribute("syndFeed", syndFeed);
		homeJsp.forward(req, resp);
	}

}